<?php
/**
 * @Author:      dx
 * @DateTime:    2016-2-16 15:32:48
 * @Description: 用于 mkt 系统用户的登录登出
 */
class MktActivitesController extends BaseController {
    /**
     * @Author:      dx
     * @DateTime:    2016-02-19 09:59:41
     * @Description: 创建市场内推活动
     */
    public function create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'activites_name' => 'required|max:25',
                'desc' => 'required|max:200',
                'start_date'=>'required',
                'stop_date'=>'required',
                'type'=>'required',
                'stock'=>'required',
                'images'=>'required',
                'url'=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            $mci=new MktCoreInfo();
            $mci->activites_name = Input::get('activites_name');
            $mci->type = Input::get('type');
            $mci->status = 1;
            $mci->desc = Input::get('desc');
            $mci->start_date = Input::get('start_date');
            $mci->stop_date = Input::get('stop_date');
            $mci->create_by = $mkt_user->id;
            $mci->save();

            $mr = new MktRule();
            $mr->mkt_core_info_id = $mci->id;
            $mr->used_start_date = Input::get('used_start_date');
            $mr->used_stop_date = Input::get('used_stop_date');
            $mr->stock = Input::get('stock');
            $mr->limit = Input::get('limit');
            $mr->limit_num = Input::get('limit_num');
            $mr->activites_type = Input::get('activites_type');
            $mr->shop_type = Input::get('shop_type');
            $mr->activites_city = Input::get('activites_city');
            $mr->activites_shop = Input::get('activites_shop');
            $mr->save();

            $mp = new MktPosition();
            $mp->mkt_core_info_id = $mci->id;
            $mp->position = Input::get('position');
            $mp->sort = Input::get('sort');
            $mp->save();

            $mm = new MktMedia();
            $mm->mkt_core_info_id = $mci->id;
            $mm->images = Input::get('images');
            $mm->url = Input::get('url');
            $mm->save();

            $fixed_price = Input::get('fixed_price');
            $mpr = new MktProductRelation();
            $mpr->mkt_core_info_id = $mci->id;
            $mpr->price_model = Input::get('price_model');
            $mpr->fixed_price = isset($fixed_price)?Input::get('fixed_price'):0;
            $mpr->save();

            //todo
            // 定价模式建立关系  1-折扣、2-降价、3-固定价格
            if(Input::get('price_model') == 1){

            }else if (Input::get('price_model') == 2){

            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("mkt_core_info_id"=>$mci->id));
        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-02-19 09:59:41
     * @Description: 创建 普通banner
     */
    public function common_banner_create(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'activites_name' => 'required|max:25',
                'desc' => 'required|max:200',
                'start_date'=>'required',
                'stop_date'=>'required',
                'type'=>'required',
                'images'=>'required',
                'url'=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            $mci=new MktCoreInfo();
            $mci->activites_name = Input::get('activites_name');
            $mci->type = Input::get('type');
            $mci->status = 1;
            $mci->desc = Input::get('desc');
            $mci->start_date = Input::get('start_date');
            $mci->stop_date = Input::get('stop_date');
            $mci->create_by = $mkt_user->id;
            $mci->save();

            $mp = new MktPosition();
            $mp->mkt_core_info_id = $mci->id;
            $mp->position = Input::get('position');
            $mp->position_sort = Input::get('position_sort');
            $mp->sort = Input::get('sort');
            $mp->save();

            $mm = new MktMedia();
            $mm->mkt_core_info_id = $mci->id;
            $mm->images = Input::get('images');
            $mm->url = Input::get('url');
            $mm->save();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("mkt_core_info_id"=>$mci->id));
        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "common_banner_create", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-02-22 11:38:37
     * @Description: 市场活动管理 活动列表
     */
    public  function activites_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);

            $activites = MktCoreInfo::with('mkt_position','mkt_rule')->orderBy('id','desc');

            if(Input::get("activites_name")){
                $activites->where("activites_name",'like','%'.Input::get("activites_name").'%');
            }
            if(Input::get("type")){
                $activites->where("type",Input::get("type"));
            }
            if(Input::get("status")){
                $activites->where("status",Input::get("status"));
            }
            if(Input::get("start_date")){
                $activites->where("start_date",Input::get("start_date"));
            }
            if(Input::get("stop_date")){
                $activites->where("stop_date",Input::get("stop_date"));
            }

            $count=$activites->count();
            $activites=$activites->skip($page_size*($page-1))->take($page_size)->get();
            $arr_activites = array();
            foreach ($activites as $_a) {
                $arr_activites[]=array(
                    "activites_name"=>$_a->activites_name,
                    "type"=> MktCoreInfo::$type[$_a->type],
                    "status"=> MktCoreInfo::$status[$_a->status],
                    "start_date"=>$_a->start_date,
                    "stop_date"=>$_a->stop_date,
                    "sort" => $_a->mkt_position->sort,
                    "position" => MktCoreInfo::$position[$_a->mkt_position->position],
                    "stock" => $_a->mkt_rule->stock,
                    "reservation" => 1,
                    "order_service" => 10,

                );
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"activites"=>$arr_activites));

        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "activites_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-02-22 11:38:37
     * @Description: 市场活动管理 商品展示位置列表
     */
    public  function position_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'position' => 'required',
                'position_sort' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            $position = MktPosition::leftjoin('mkt_core_info','mkt_core_info.id','=','mkt_position.mkt_core_info_id')
                ->whereIn('mkt_core_info.status',array(1,2,4))
                ->where('mkt_position.position',Input::get('position'))
                ->where('mkt_position.position_sort',Input::get('position_sort'))
                ->orderBy('mkt_position.sort','asc')
                ->orderBy('mkt_core_info.start_date','desc')
                ->select('mkt_core_info.activites_name','mkt_core_info.start_date','mkt_core_info.stop_date','mkt_position.sort')
                ->get();

            $result = array(
                'position' => MktCoreInfo::$position[Input::get('position')],
                'position_sort' => Input::get('position_sort'),
                'position_list' => $position
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);

        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "position_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-02-25 11:38:37
     * @Description: 市场活动管理 活动城市范围及门店关系  门店
     */
    public  function city_shops_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_type' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }

            $shops = MaintenanceShops::leftjoin("resource_province_type","resource_province_type.id","=",'maintenance_shops.city_id')
                ->where("maintenance_shops.shop_type",Input::get('shop_type'))
                ->where('maintenance_shops.status',1)
                ->where('resource_province_type.status',1)
                ->select("maintenance_shops.name","maintenance_shops.id",'resource_province_type.capital','maintenance_shops.city_id')->get();
            $shops_list = array();
            $shops_list2 = array();
            foreach($shops as $_s){
                $temp_arr = array();
                $temp_arr['city_name'] = $_s->capital;
                $temp_arr['city_id'] = $_s->city_id;
                $shops_list2[$_s->city_id][$_s->id] =  $_s->name;
                $shops_list[$_s->city_id] = $temp_arr;
                $shops_list[$_s->city_id]['shop'] = $shops_list2[$_s->city_id];
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $shops_list);

        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "city_shops_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      dx
     * @DateTime:    2016-02-26 11:38:37
     * @Description: 市场活动管理 上门车
     */
    public  function cars_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("mkt_error.args_error_100000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $mkt_user=$this->verificationMKTUserToken(Input::get("token"),$response);
            if(is_null($mkt_user)){
                break;
            }
            $cars = MaintenanceCars::leftjoin("resource_province_type","resource_province_type.id","=",'maintenance_cars.city_id')
                ->where("maintenance_cars.status",1)
                ->where('resource_province_type.status',1)
                ->select('maintenance_cars.city_id','resource_province_type.capital')
                ->distinct()
                ->get();

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $cars);

        }while(false);
        Log::info("info", array("system" => "mkt" ,"event" => "cars_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


}